import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { WinRabbitMoneyStrategyV2 } from "./WinRabbitMoneyStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { NormalGroupMoneyStrategy } from "./NormalGroupMoneyStrategy";
import { WisdomGroupMoneyStrategyV2 } from "./WisdomMoneyStrategyV2";
import { MaxBossRabbitMoneyStrategy } from "./BossRabitMoneyStrategy";
import { WithRecmMoneyStrategy } from "./WithRecmMoneyStrategy";
import { MaxStepMoneyhStrategy, NormalStepMoneyStrategy } from "./NormalStepMoneyStrategy";


export class WisdomMoneyStrategyV29Group4 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV2Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sum = 0;


        this.subPlan.forEach(pl => {;
            let [rv, rm] = pl.showMoney();
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            
            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {

        let gi = new ExcutePlan();
        gi.planName = `正2`;
        gi.baseMoney = 10;
        gi.recmStrategy = [new FixedStrategy({ reverse: false })];
        gi.relationPindex = [this.subPlan.length + 1];
        gi.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$wisdom-group-1`, {
            groupCount: 2,
            addStep: 10,
            devideStep: -10,
            reverse: false, //true:赢了+100，输了-100
            maxLevel: 300000000,
        })]
        gi.toResult(new RoundInfo());
        this.subPlan.push(gi);

        let gif = new ExcutePlan();
        gif.planName = `反2`;
        gif.baseMoney = 10;
        gif.relationPindex = [this.subPlan.length - 1];
        gif.recmStrategy = [new FixedStrategy({ reverse: true })];
        gif.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$reverse-wisdom-group-1`, {
            groupCount: 2,
            addStep: 10,
            devideStep: -10,
            reverse: false, //true:赢了+100，输了-100
            maxLevel: 300000000
        })]
        gif.toResult(new RoundInfo());
        this.subPlan.push(gif);

        var pgPlan1 = new ExcutePlan();
        pgPlan1.planName = "复正";
        pgPlan1.recmStrategy = [new FixedStrategy({ reverse: false })];
        pgPlan1.moneyStrategy = [new NormalStepMoneyStrategy("方案一", {
            baseMoney: 10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true)];
        pgPlan1.toResult(new RoundInfo());
        this.subPlan.push(pgPlan1);

        var pgfPlan1 = new ExcutePlan();
        pgfPlan1.planName = "复反";
        pgfPlan1.recmStrategy = [new FixedStrategy({ reverse: true })];
        pgfPlan1.moneyStrategy = [new NormalStepMoneyStrategy("方案二", {
            baseMoney: 10,
            startMoney: 10,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            resetLevel: 80,
        }, true)];
        pgfPlan1.toResult(new RoundInfo());
        this.subPlan.push(pgfPlan1);

    }
    setSubResult(pl: ExcutePlan, roundInfo: RoundInfo) {
        pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);

        let m = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
        this._subPlanSum += m;

        let userRecmV = pl.toResult(roundInfo);
        let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
        pl.userRecmInfo.recmV = userRecmV;
        return newRoundInfo;
    }
    toSubMoney(pl: ExcutePlan, roundInfo: RoundInfo) {
        if (pl.running) {
            let [rv, rm] = pl.toMoney(roundInfo, pl.recmInfo, pl.userRecmInfo, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }
        }
        else {
            pl.recmInfo.recmV = EnumBjlResult.U;
            pl.recmInfo.recmMoney = 0;
        }

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;

        let reset = false;
        this._subPlanSum = sumWin;

        let giplan = this.subPlan[0];
        let gifplan = this.subPlan[1];
        let pgplan = this.subPlan[2];
        let pgfplan = this.subPlan[3];

        let giRoundInfo = this.setSubResult(giplan, roundInfo);
        this.toSubMoney(giplan, giRoundInfo);

        let gifRoundInfo = this.setSubResult(gifplan, roundInfo);
        this.toSubMoney(gifplan, gifRoundInfo);


        let giMoneyStrategy = giplan.moneyStrategy[0] as WisdomGroupMoneyStrategyV2;
        let gifMoneyStrategy = gifplan.moneyStrategy[0] as WisdomGroupMoneyStrategyV2;
        let pgplanMoneyStratgy = pgplan.moneyStrategy[0] as NormalStepMoneyStrategy;
        let pgfplanMoneyStratgy = pgfplan.moneyStrategy[0] as NormalStepMoneyStrategy;


        if (giMoneyStrategy.levelChange) {
            pgfplanMoneyStratgy.resetStrategy();

            pgfplanMoneyStratgy.setBaseMoney(giMoneyStrategy.currentRecmMoney);
            pgplan.running = false;
        }

        if (gifMoneyStrategy.levelChange) {
            pgplanMoneyStratgy.resetStrategy();
            pgplanMoneyStratgy.setBaseMoney(gifMoneyStrategy.currentRecmMoney);

            pgfplan.running = false;
        }

        if (giMoneyStrategy.currentRecmMoney <= giplan.baseMoney) {
            pgplan.running = true;
        }

        if (gifMoneyStrategy.currentRecmMoney <= gifplan.baseMoney) {
            pgfplan.running = true;
        }

        let pgplanRoundInfo = this.setSubResult(pgplan, roundInfo);
        this.toSubMoney(pgplan, pgplanRoundInfo);

        let pgpflanRoundInfo = this.setSubResult(pgfplan, roundInfo);
        this.toSubMoney(pgfplan, pgpflanRoundInfo);

        this.subPlan.forEach(pl => {
            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
            }
        })

        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV29Group4;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            this._subPlanSum = obj._subPlanSum;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}